package com.mihoyo.org.controller;

import com.mihoyo.basic.utils.AjaxResult;
import com.mihoyo.basic.utils.PageList;
import com.mihoyo.org.domain.Department;
import com.mihoyo.org.domain.Employee;
import com.mihoyo.org.query.DepartmentQuery;
import com.mihoyo.org.service.IDepartmentService;
import org.apache.ibatis.javassist.compiler.ast.Keyword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dept")
@RestController // 等于Controller + ResponseBody
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    public List<Department> findAll() {
       return departmentService.findAll();
    }


    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    // 添加或者修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (department.getId() == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping
    public PageList<Department> queryByPage (@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryByPage(departmentQuery);
    }

    // 批量删除
    @PatchMapping
    public AjaxResult batchDepartment(@RequestBody List<Long> ids) {
        departmentService.batchDelete(ids);
        return new AjaxResult().success();
    }

    // 部门无限极数
    @GetMapping("/tree")
    public List<Department> findDepartmentTree() {
        List<Department> departmentTree = departmentService.findDepartmentTree();
        return departmentTree;
    }



}
